package com.demo.las.controller;

import com.demo.las.dto.ApiResponse;
import com.demo.las.entity.StudyRecord;
import com.demo.las.service.StudyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/study-records")
@CrossOrigin(origins = "*")
public class StudyRecordController {

    @Autowired
    private StudyRecordService studyRecordService;

    @GetMapping
    public ApiResponse<List<StudyRecord>> getAllStudyRecords(@RequestParam Long userId) {
        List<StudyRecord> records = studyRecordService.getStudyRecordsByUser(userId);
        return ApiResponse.success(records);
    }

    @GetMapping("/{id}")
    public ApiResponse<StudyRecord> getStudyRecordById(@PathVariable Long id) {
        return studyRecordService.getStudyRecordById(id)
                .map(record -> ApiResponse.success(record))
                .orElse(ApiResponse.error(404, "学习记录不存在"));
    }

    @PostMapping
    public ApiResponse<StudyRecord> createStudyRecord(@Valid @RequestBody StudyRecord studyRecord) {
        try {
            StudyRecord createdRecord = studyRecordService.createStudyRecord(studyRecord);
            return ApiResponse.success("学习记录创建成功", createdRecord);
        } catch (Exception e) {
            return ApiResponse.error("创建学习记录失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ApiResponse<StudyRecord> updateStudyRecord(@PathVariable Long id, @Valid @RequestBody StudyRecord studyRecord) {
        try {
            studyRecord.setId(id);
            StudyRecord updatedRecord = studyRecordService.updateStudyRecord(studyRecord);
            return ApiResponse.success("学习记录更新成功", updatedRecord);
        } catch (Exception e) {
            return ApiResponse.error("更新学习记录失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteStudyRecord(@PathVariable Long id) {
        try {
            studyRecordService.deleteStudyRecord(id);
            return ApiResponse.success("学习记录删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除学习记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-subject")
    public ApiResponse<List<StudyRecord>> getStudyRecordsBySubject(
            @RequestParam Long userId,
            @RequestParam String subject) {
        List<StudyRecord> records = studyRecordService.getStudyRecordsByUserAndSubject(userId, subject);
        return ApiResponse.success(records);
    }

    @GetMapping("/by-date-range")
    public ApiResponse<List<StudyRecord>> getStudyRecordsByDateRange(
            @RequestParam Long userId,
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        List<StudyRecord> records = studyRecordService.getStudyRecordsByDateRange(userId, startDate, endDate);
        return ApiResponse.success(records);
    }

    @GetMapping("/statistics")
    public ApiResponse<StudyRecordService.StudyStatistics> getStudyStatistics(
            @RequestParam Long userId,
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        StudyRecordService.StudyStatistics stats = studyRecordService.getStudyStatistics(userId, startDate, endDate);
        return ApiResponse.success(stats);
    }

    @GetMapping("/total-time")
    public ApiResponse<BigDecimal> getTotalStudyTime(
            @RequestParam Long userId,
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        BigDecimal totalTime = studyRecordService.getTotalStudyTime(userId, startDate, endDate);
        return ApiResponse.success(totalTime);
    }

    @GetMapping("/time-by-subject")
    public ApiResponse<List<Object[]>> getStudyTimeBySubject(
            @RequestParam Long userId,
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        List<Object[]> timeBySubject = studyRecordService.getStudyTimeBySubject(userId, startDate, endDate);
        return ApiResponse.success(timeBySubject);
    }

    @GetMapping("/daily-trend")
    public ApiResponse<List<Object[]>> getDailyStudyTimeTrend(
            @RequestParam Long userId,
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        List<Object[]> dailyTrend = studyRecordService.getDailyStudyTimeTrend(userId, startDate, endDate);
        return ApiResponse.success(dailyTrend);
    }

    @GetMapping("/efficiency-index")
    public ApiResponse<BigDecimal> getEfficiencyIndex(
            @RequestParam Long userId,
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        BigDecimal efficiencyIndex = studyRecordService.calculateEfficiencyIndex(userId, startDate, endDate);
        return ApiResponse.success(efficiencyIndex);
    }
}




